/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer.contacts.adapters;

import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import nl.sidn.enumdiscoverer.EnumRecord;
import nl.sidn.enumdiscoverer.LogConfig;
import nl.sidn.enumdiscoverer.contacts.ContactProfileAggregator;
import android.text.TextUtils;
import android.util.Log;

/**
 * Parse X-ADDRESS ENUM records and store data appropriately.
 * @author	Alan McNatty (alan.mcnatty@catalyst.net.nz)
 */
public class XAddressAdapter implements ContactAdapter {
	
	static final String TAG = LogConfig.getLogTag(XAddressAdapter.class);
	// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
	static final boolean DEBUG = LogConfig.isDebug(TAG);
	// whether VERBOSE level logging is enabled
	static final boolean VERBOSE = LogConfig.VERBOSE;
	
	@Override
	public void updateContact(String dataCapture, EnumRecord enumRecord,
			ContactProfileAggregator profileAggregator) {

		if (VERBOSE) Log.v(TAG, "x-address dataCapture = " + dataCapture);
		
		/* Temporary address buffer - built from parts */
		String address = "";
		
		/* remove escaping */
		dataCapture.replaceAll("\\\\", "");
		/* tokenise on field delimiter ';' */
		StringTokenizer st = new StringTokenizer(dataCapture, ";");
		

		while ( true ) { 
			try {
				String nt = st.nextToken();
			
				if (VERBOSE) Log.v(TAG, "Found token '" + nt + "'");

				StringTokenizer ft = new StringTokenizer(nt, "=");
			
				String f=ft.nextToken(); 
				String v=ft.nextToken(); 

				if (VERBOSE) Log.v(TAG, "Found field='" + f + "', value='" + v + "'");
				
				if ( f.equals("CN")) {
					profileAggregator.addName(v, enumRecord);
					/* (L)ocality, (S)tate or (C)ountry 
					 * are the only examples found so far, extend as necessary
					 * Note: they should split in order so just concatenate */
				} else if ( f.matches("l|L|st|ST|c|C")) {
					address += v + "\n";
				} else if ( f.equals("O")) {
					profileAggregator.addOrganisation(v, enumRecord);
				} else {
					Log.w(TAG, "Unrecognised field '" + f + "' with value '" + v + "'");
				}
			} catch (NoSuchElementException e) {
				/* Log.w(TAG, "Error parsing X-ADDRESS", e); */
				break;
			}
		} 
		
		if ( !TextUtils.isEmpty(address) ) {
			profileAggregator.addAddress(address, enumRecord);
		}
	}
}
